#!/bin/bash 
set -e

OS=$(uname -s)

# 判断操作系统类型
# if [ "$OS" = "Linux" ]; then
#     project_dir=$(cd $(dirname $0);pwd -P)
# elif [ "$OS" = "Windows_NT" ]; then # 注意：只在Cygwin下有效
#     project_dir=$(cd $(dirname $0);pwd -P)
# else
#     echo "未知操作系统: $OS"
# fi
project_dir="E:\\wwwroot\\laravel_devops"
project_dir=$(echo $project_dir | sed 's/\\/\\\\/g')

#### 定义项目的一些基本的路径信息内容
#### 定义一些目录信息
project_devops_dir="$project_dir\\\\devops"
project_devops_nginx_dir="$project_devops_dir\\\\nginx"
project_devops_nginx_log_dir="$project_devops_dir\\\\logs\\\\nginx"
project_devops_php_dir="$project_devops_dir\\\\php"
project_devops_php_log_dir="$project_devops_dir\\\\logs\\\\php"
project_devops_mysql_dir="$project_devops_dir\\\\mysql\\\\data"
project_devops_mysql_log_dir="$project_devops_dir\\\\logs\\\\mysql"
project_devops_web_dir="$project_dir\\\\www";

#### 定义要拉取的镜像信息
php_img_name="php8-image:2.0"
mysql_img="mysql:5.7"
nginx="nginx"
redis="redis"
mysql_db_name="laravel_db"
mysql_port="33062"
redis_port="6381"
nginx_port="10010"

#### 定义容器的名字
php_container="laravel_container"
mysql_container="laravel_mysql"
nginx_container="laravel_nginx"
redis_container="laravel_redis"

#### 初始化打包自己的PHP7环境镜像信息
function build_php(){
    run_cmd "docker build -t $php_img_name ."
}

function init_app(){
    run_mysql
    run_redis
    run_php
    run_nginx
    # init_laravel
}

function rm_app(){
    rm_mysql
    rm_redis
    rm_php
    rm_nginx
}

function list_container(){
    run_cmd "docker ps -a -f name='^laravel'"
}

#### 运行mysql相关的内容操作
function run_mysql(){
    local args="--restart always"
    args="$args -v $project_devops_mysql_dir:/var/lib/mysql"
    args="$args -v $project_devops_mysql_log_dir:/var/log/mysql"
    args="$args -p $mysql_port:3306"
    args="$args -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASE=$mysql_db_name -e MYSQL_ALLOW_EMPTY_PASSWORD='yes'"

    run_cmd "docker run -itd $args --name $mysql_container $mysql_img";
}

function rm_mysql(){
    run_cmd "docker stop $mysql_container && docker rm -f $mysql_container"
}

function restart_mysql(){
    rm_mysql
    run_mysql
}

function to_mysql(){
    cmd="mysql -u root -h 127.0.0.1 -P 3306 -p123456"
    run_cmd "docker exec -it $mysql_container bash -c '$cmd'"
}

#### Redis相关的容器
function run_redis(){
    local args="--restart always"
    args="$args -p $redis_port:6379"
    run_cmd "docker run -itd $args --name $redis_container $redis"
}

function rm_redis(){
    run_cmd "docker stop $redis_container && docker rm -f $redis_container"
}

function restart_redis(){
    rm_redis
    run_redis
}

function to_redis(){
    local cmd="redis-cli"
    run_cmd "docker exec -it $redis_container bash -c '$cmd'"
}

#### php容器相关的命令
function run_php(){
    local args="--restart always"
    args="$args -v $project_devops_php_dir:/usr/local/etc"
    # args="$args -v $project_devops_php_log_dir:/var/log/php"
    args="$args -v $project_devops_web_dir:/var/www/html"
    args="$args --link $redis_container --link $mysql_container"
    run_cmd "docker run -itd $args --name $php_container $php_img_name"
}

function rm_php(){
     run_cmd "docker stop $php_container && docker rm -f $php_container"
}

function restart_php(){
    rm_php
    run_php
}
function to_php(){
    run_cmd "docker exec -it $php_container bash "
}

function init_laravel(){
    cmd="apt-get update&& apt-get install -y --no-install-recommends libfreetype6-dev libjpeg62-turbo-dev libpng-dev&& docker-php-ext-configure gd && docker-php-ext-install gd;";
    cmd="$cmd cd /var/www/html;"
    cmd="$cmd php artisan migrate;"
    cmd="$cmd php artisan db:seed;"
    run_cmd "docker exec -it $php_container bash -c '$cmd'"
}

#### nginx容器相关的启动操作
function run_nginx(){
    local args="--restart always"
    args="$args -v $project_devops_nginx_dir:/etc/nginx"
    args="$args -v $project_devops_nginx_log_dir:/var/log/nginx"
    args="$args -v $project_devops_web_dir:/usr/share/nginx/html"
    args="$args -p $nginx_port:80"
    args="$args --link $php_container"

    run_cmd "docker run -itd $args --name $nginx_container $nginx"
}

function rm_nginx(){
    run_cmd "docker stop $nginx_container && docker rm -f $nginx_container"
}

function restart_nginx(){
    rm_nginx
    run_nginx
}
function to_nginx(){
    run_cmd "docker exec -it $nginx_container bash";
}

### 定义执行命令的方法操作
function run_cmd() {
    local t=`date`
    echo "$t: $1"
    eval $1
}

#### 运行mysql 容器的信息操作

#### 运行nginx 容器相关的信息操作

#### 运行php 项目环境相关的操作信息

#### 此处是操作命令的对象信息
function help() {
cat <<EOF
    Usage: manage.sh [options]
    build_php   //打包PHP7的镜像信息
    init_app
    rm_app
    list_container //查看相关容器

    //数据库相关操作
    run_mysql
    rm_mysql
    restart_mysql
    to_mysql

    run_redis
    rm_redis
    to_redis
    restart_redis

    run_php
    rm_php
    to_php
    restart_php
    init_laravel

    run_nginx
    rm_nginx
    to_nginx
    restart_nginx

    help    show this message
EOF
}

function list_contains() {
    local var="$1"
    local str="$2"
    local val

    eval "val=\" \${$var} \""
    [ "${val%% $str *}" != "$val" ]
}
### 如果没有做选择就是help
action=${1:-help}
#### 执行命令的基本操作，
ALL_COMMANDS="run_mysql rm_mysql restart_mysql to_mysql"
ALL_COMMANDS="$ALL_COMMANDS run_redis rm_redis to_redis restart_redis"
ALL_COMMANDS="$ALL_COMMANDS run_php rm_php to_php restart_php init_laravel"
ALL_COMMANDS="$ALL_COMMANDS run_nginx rm_nginx to_nginx restart_nginx"
ALL_COMMANDS="$ALL_COMMANDS init_app rm_app list_container build_php"
list_contains ALL_COMMANDS "$action" || action=help
$action "$@"